package com.crm.service.impl.cluePoolManager;

import com.alibaba.fastjson.JSONObject;
import com.crm.common.util.CommonUtils;
import com.crm.dao.mapper.cluePoolManager.ClueReceiveMapper;
import com.crm.dao.mapper.customMapper.cluePoolManagerCustomMapper.ClueReceiveCustomMapper;
import com.crm.model.entity.cluePoolManager.CluePoolRule;
import com.crm.model.entity.cluePoolManager.ClueReceive;
import com.crm.model.entity.cluePoolManager.ClueReceiveExample;
import com.crm.model.entity.sys.UserAccount;
import com.crm.model.vo.cluePoolManager.ClueReceiveVO;
import com.crm.service.cluePoolManager.ClueReceiveDbService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.*;

/**
 * @ Author ：pym
 * @ Date   ：2021/1/8 13:03
 */
@Service
public class ClueReceiveDbServiceImpl implements ClueReceiveDbService {

    @Autowired
    ClueReceiveCustomMapper clueReceiveCustomMapper;
    @Autowired
    ClueReceiveMapper clueReceiveMapper;

    /**
     * 线索池详情-线索清单-领取记录
     * @param clueId        ---线索Id
     * @return
     */
    @Override
    public List<ClueReceiveVO> selectClueReceiveVOList(String clueId) {
        List<ClueReceiveVO> clueReceiveVOList = clueReceiveCustomMapper.selectClueReceiveVOList(clueId);
        return clueReceiveVOList;
    }

    /**
     *新增一条领取/分配记录
     * @param fromId       ---原线索池id
     * @param toId         ---新线索池id
     * @param clueId       ---线索ID
     * @param accountId    ---被分配/领取人ID
     * @param receiveWay   ---领取方式：1-领取；2:分配
     * @return
     */
    public Boolean insertClueReceive(String fromId,String toId,String clueId,String accountId,Integer receiveWay){
        ClueReceive clueReceive = new ClueReceive();
        clueReceive.setClueReceiveId(CommonUtils.getGUID());//主键
        clueReceive.setReceivePeopleId(accountId);//被分配人员ID/领取人ID
        clueReceive.setReceiveTm(new Date());//分配时间
        clueReceive.setReceiveWay(receiveWay);//领取方式---1:领取;2:分配)
        clueReceive.setClueId(clueId);//线索ID
        clueReceive.setCluePoolFromId(fromId);//原线索池id
        clueReceive.setCluePoolToId(toId);//新线索池id
        return clueReceiveMapper.insert(clueReceive) > 0;
    }


    /**
     * @author wll
     * info 同一线索池线索的领取时间间隔是否满足
     */
    @Override
    public JSONObject TimeIntervalFromLastClue(String poolId,Integer continueTimeInterval,UserAccount userAccount) {
        JSONObject result = new JSONObject();
        ClueReceiveExample clueReceiveExample = new ClueReceiveExample();
        ClueReceiveExample.Criteria criteria = clueReceiveExample.createCriteria();
        criteria.andReceivePeopleIdEqualTo(userAccount.getAccountId());//领取人id
        criteria.andReceiveWayEqualTo(1);//领取方式：1-领取；2:分配
        criteria.andCluePoolFromIdEqualTo(poolId);//线索池id: 从哪个池子领取的池子id
        clueReceiveExample.setOrderByClause("receive_tm desc");
        List<ClueReceive> clueReceives = clueReceiveMapper.selectByExample(clueReceiveExample);
        if(null !=clueReceives && clueReceives.size()>0){
            Date receiveTm = clueReceives.get(0).getReceiveTm();
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(receiveTm);
            calendar.add(Calendar.SECOND, continueTimeInterval);
            receiveTm = calendar.getTime();
            Date current_time = new Date();
            if(receiveTm.before(current_time)){
                result.put("status",true);
                return result;
            }
            result.put("toast","线索领取过于频繁");
            result.put("status",false);
            return result;
        }else{
            result.put("status",true);
            return result;
        }
    }


    /**
     * @author wll
     * info 线索领取量是否超过该池设置的最大领取量
     */
    @Override
    public JSONObject selectClueReceiveInfo(UserAccount userAccount, String poolId, CluePoolRule cluePoolRuleInfo) {
        JSONObject result = new JSONObject();
        ClueReceiveExample clueReceiveExample = new ClueReceiveExample();
        ClueReceiveExample.Criteria criteria = clueReceiveExample.createCriteria();
        criteria.andReceivePeopleIdEqualTo(userAccount.getAccountId());//领取人id
        criteria.andReceiveWayEqualTo(1);//领取方式：1-领取；2:分配
        criteria.andCluePoolFromIdEqualTo(poolId);//线索池id: 从哪个池子领取的池子id
        long ClueReceiveNum = Integer.parseInt(String.valueOf(clueReceiveMapper.countByExample(clueReceiveExample)));
        if(ClueReceiveNum >= cluePoolRuleInfo.getCluePoolNumber()){//线索领取量超过该池设置的最大领取量
            result.put("toast","超过该池最大领取量");
            result.put("status",false);
            return result;
        }else{
            result.put("status",true);
            return result;
        }
    }

    @Override
    public long selectReceiveCountForCurrentMonthByPoolId(String cluePoolId, String yearMonth) {
        long num = clueReceiveCustomMapper.selectReceiveCountForCurrentMonthByPoolId(cluePoolId,yearMonth);
        return num;
    }

    @Override
    public long selectClueReceiveByPoolId(String cluePoolId) {
        ClueReceiveExample clueReceiveExample = new ClueReceiveExample();
        ClueReceiveExample.Criteria criteria = clueReceiveExample.createCriteria();
        criteria.andCluePoolFromIdEqualTo(cluePoolId);
        long num = clueReceiveMapper.countByExample(clueReceiveExample);
        return num;
    }

    /**
     *(线索清单)线索领取次数
     * @param clueId ---线索ID
     * @return
     */
    public long selectReceiveCountByClueId(String clueId){
        return clueReceiveCustomMapper.selectReceiveCountByClueId(clueId);
    }
}
